home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Libraries / tk / tkwindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  6.4 KB  |  327 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "tk.h"
  6. #include "tkprivate.h"
  7. #include "tkmacglobals.h"
  8. #include "tkcolormaps.h"
  9.  
  10. #if defined(__MWERKS__) || defined(__SC__)
  11.     #include "console.h"
  12. #endif
  13.  
  14. #if defined(__MWERKS__)
  15.     #include "SIOUX.h"
  16. #endif
  17.  
  18. static void DoInitManagers(void);
  19. static void DoInitConsole(void);
  20. static void DoSetUpMenus(void);
  21. static void DoInit(void);
  22. static void fillAttrib(GLenum type, GLint attrib[32]);
  23. static void CToPStr(char *cs, Str255 ps);
  24.  
  25. static GLboolean   tkInitApp = GL_TRUE;
  26. tkWindowRec    tkWindow  = { 30, 30, 200, 200, 0, NULL, NULL, GL_FALSE };
  27.  
  28. float tkRGBMap[8][3] = {
  29.     { 0, 0, 0 },
  30.     { 1, 0, 0 },
  31.     { 0, 1, 0 },
  32.     { 1, 1, 0 },
  33.     { 0, 0, 1 },
  34.     { 1, 0, 1 },
  35.     { 0, 1, 1 },
  36.     { 1, 1, 1 }
  37. };
  38.  
  39. static void DoInitManagers(void)
  40. {
  41.     MaxApplZone();
  42.     MoreMasters();
  43.     
  44.     InitGraf(&qd.thePort);
  45.     InitFonts();
  46.     FlushEvents(everyEvent, 0);
  47.     InitWindows();
  48.     InitMenus();
  49.     TEInit();
  50.     InitDialogs(nil);
  51.     InitCursor();
  52. }
  53.  
  54. static void DoInitConsole(void)
  55. {
  56.     #if defined(__MWERKS__)
  57.         SIOUXSettings.initializeTB       = FALSE;
  58.         SIOUXSettings.standalone         = FALSE;
  59.         SIOUXSettings.setupmenus         = FALSE;
  60.         SIOUXSettings.autocloseonquit    = TRUE;
  61.         SIOUXSettings.asktosaveonclose   = TRUE;
  62.         SIOUXSettings.showstatusline     = TRUE;
  63.         SIOUXSettings.userwindowtitle    = NULL;
  64.         SIOUXSettings.tabspaces          = 4;
  65.         SIOUXSettings.columns            = 60;
  66.         SIOUXSettings.rows               = 8;
  67.         SIOUXSettings.toppixel           = 100;
  68.         SIOUXSettings.leftpixel          = 100;
  69.         SIOUXSettings.fontid             = kFontIDMonaco;
  70.         SIOUXSettings.fontsize           = 9;
  71.         SIOUXSettings.fontface           = normal;
  72.         SIOUXSettings.enabledraganddrop  = FALSE;
  73.         SIOUXSettings.outlinehilite      = FALSE;
  74.         SIOUXSettings.wasteusetempmemory = TRUE;
  75.     #endif
  76. }
  77.  
  78. static void DoSetUpMenus(void)
  79. {
  80.     Handle menuBar;
  81.     
  82.     menuBar = GetNewMBar(rMenuBar);
  83.     if(!menuBar) printf("TK error: MenuBar not found.\n");
  84.     
  85.     SetMenuBar(menuBar);
  86.     DisposeHandle(menuBar);
  87.     AppendResMenu(GetMenuHandle(mApple),'DRVR');
  88.     DrawMenuBar();
  89. }
  90.  
  91. static void DoInit(void)
  92. {
  93.     DoInitConsole();
  94.     DoInitManagers();
  95.     DoSetUpMenus();
  96.     
  97.     gDone = false;
  98. }
  99.  
  100. void tkCloseWindow(void)
  101. {
  102.     if(tkWindow.context)  aglDestroyContext(tkWindow.context);
  103.     if(tkWindow.drawable) DisposeWindow((GrafPort *) tkWindow.drawable);
  104.     if(tkWindow.fmt)      aglDestroyPixelFormat(tkWindow.fmt);
  105.     
  106.     tkWindow.x             = 30;
  107.     tkWindow.y             = 30;
  108.     tkWindow.width         = 200;
  109.     tkWindow.height        = 200;
  110.     tkWindow.type          = 0;
  111.     tkWindow.context       = NULL;
  112.     tkWindow.drawable      = NULL;
  113.     tkWindow.fmt           = NULL;
  114.     tkWindow.double_buffer = GL_FALSE;
  115. }
  116.  
  117. void tkQuit(void)
  118. {
  119.     gDone = TRUE;
  120.     tkCloseWindow();
  121. }
  122.  
  123. void tkInitDisplayMode(GLenum type)
  124. {
  125.     tkWindow.type = type;
  126. }
  127.  
  128. void tkInitPosition(int x, int y, int width, int height)
  129. {
  130.     tkWindow.x = x;
  131.     tkWindow.y = y;
  132.     tkWindow.width = width;
  133.     tkWindow.height = height;
  134. }
  135.  
  136. void tkSwapBuffers(void)
  137. {
  138.     if(!tkWindow.drawable && !tkWindow.fullscreen) printf("TK error: no window defined\n");
  139.     
  140.     if(tkWindow.double_buffer) aglSwapBuffers(aglGetCurrentContext());
  141. }
  142.  
  143. static void fillAttrib(GLenum type, GLint attrib[32])
  144. {
  145.     GLushort i = 0; 
  146.     
  147.     if (!(type & TK_INDEX))
  148.     {
  149.         attrib[i++] = AGL_RGBA;
  150.     }
  151.     
  152.     if ((type & TK_DOUBLE))
  153.     {
  154.         attrib[i++] = AGL_DOUBLEBUFFER;
  155.         tkWindow.double_buffer = true;
  156.     }
  157.     else
  158.     {
  159.         tkWindow.double_buffer = false;
  160.     }
  161.     
  162.     if (type & TK_OVERLAY)
  163.     {
  164.         attrib[i++] = AGL_LEVEL;
  165.         attrib[i++] = 0;
  166.     }
  167.     
  168.     if (type & TK_UNDERLAY)
  169.     {
  170.         attrib[i++] = AGL_LEVEL;
  171.         attrib[i++] = 0;
  172.     }
  173.     
  174.     if (type & TK_STENCIL)
  175.     {
  176.         attrib[i++] = AGL_STENCIL_SIZE;
  177.         attrib[i++] = 1;
  178.     }
  179.     
  180.     if (type & TK_DEPTH)
  181.     {
  182.         attrib[i++] = AGL_DEPTH_SIZE;
  183.         attrib[i++] = 24;
  184.     }
  185.     
  186.     if (type & TK_ACCUM)
  187.     {
  188.         attrib[i++] = AGL_ACCUM_RED_SIZE;
  189.         attrib[i++] = 16;
  190.         attrib[i++] = AGL_ACCUM_GREEN_SIZE;
  191.         attrib[i++] = 16;
  192.         attrib[i++] = AGL_ACCUM_BLUE_SIZE;
  193.         attrib[i++] = 16;
  194.         
  195.         if(type & TK_ALPHA)
  196.         {
  197.             attrib[i++] = AGL_ACCUM_ALPHA_SIZE;
  198.             attrib[i++] = 16;
  199.         }
  200.     }
  201.     
  202.     if (type & TK_ALPHA)
  203.     {
  204.         attrib[i++] = AGL_ALPHA_SIZE;
  205.         attrib[i++] = 1;
  206.     }
  207.     
  208.     if (type & TK_AUX)
  209.     {
  210.         attrib[i++] = AGL_AUX_BUFFERS;
  211.         attrib[i++] = 1;
  212.     }
  213.     
  214.     if (type & TK_FULLSCREEN)
  215.     {
  216.         attrib[i++] = AGL_FULLSCREEN;
  217.     }
  218.     
  219.     attrib[i++] = AGL_ALL_RENDERERS;
  220.     
  221.     attrib[i++] = 0;
  222. }
  223.  
  224. void CToPStr(char *cs, Str255 ps)
  225. {
  226.     GLint i, l;
  227.     
  228.     l = strlen(cs);
  229.     if(l > 255) l = 255;
  230.     ps[0] = l;
  231.     for(i = 0; i < l; i++) ps[i + 1] = cs[i];
  232. }
  233.  
  234. GLenum tkInitWindow(char *title)
  235. {
  236.     GLint     attrib[32];
  237.     Str255    pstr;
  238.     GLint     value;
  239.  
  240.     /* Check if a window is already open */
  241.     if(tkWindow.drawable)
  242.     {
  243.         printf("TK error: window already defined\n");
  244.         exit(1);
  245.     }
  246.  
  247.     /* Init application once */
  248.     if(tkInitApp)
  249.     {
  250.         DoInit();
  251.         tkInitApp = GL_FALSE;
  252.     }
  253.     
  254.     /* Read attribute list */
  255.     fillAttrib(tkWindow.type, attrib);
  256.  
  257.     /* Choose pixel format */
  258.     tkWindow.fmt = aglChoosePixelFormat(NULL, 0, attrib);
  259.     if(!tkWindow.fmt) 
  260.     {
  261.         printf("TK error: Pixel format could not be achieved\n");
  262.         exit(1);
  263.     }
  264.     
  265.     aglDescribePixelFormat(tkWindow.fmt, AGL_FULLSCREEN, &value);
  266.     tkWindow.fullscreen = value;
  267.  
  268.     /* Create context */
  269.     tkWindow.context = aglCreateContext(tkWindow.fmt, NULL);
  270.     if(!tkWindow.context)
  271.     {
  272.         printf("TK error: Context could not be created\n");
  273.         exit(1);
  274.     }
  275.     
  276.     /* Make context current */
  277.     if(!tkWindow.fullscreen)
  278.     {
  279.         /* Create window */
  280.         tkWindow.drawable = (AGLDrawable) GetNewCWindow(kMainWindow,nil,(WindowPtr)-1L);
  281.         if(!tkWindow.drawable)
  282.         {
  283.             printf("TK error: window could not be created\n");
  284.             exit(1);
  285.         }
  286.         
  287.         CToPStr(title, pstr);
  288.         SetWTitle((GrafPort *) tkWindow.drawable, pstr);
  289.         SizeWindow((GrafPort *) tkWindow.drawable,tkWindow.width,tkWindow.height,GL_FALSE);
  290.         MoveWindow((GrafPort *) tkWindow.drawable,tkWindow.x,tkWindow.y,GL_FALSE);
  291.         ShowWindow((GrafPort *) tkWindow.drawable);
  292.         HiliteWindow((GrafPort *) tkWindow.drawable, TRUE);
  293.         SetPort((GrafPort *) tkWindow.drawable);
  294.         
  295.         if(!aglSetDrawable(tkWindow.context, tkWindow.drawable))
  296.         {
  297.             printf("TK error: Could not attach to context\n");
  298.             exit(1);
  299.         }
  300.     }
  301.     else
  302.     {
  303.         if(!aglSetFullScreen(tkWindow.context, tkWindow.width, tkWindow.height, 60, 0))
  304.         {
  305.             printf("TK error: Could not attach to context\n");
  306.             exit(1);
  307.         }
  308.         
  309.         tkWindow.x = 0;
  310.         tkWindow.y = 0;
  311.         
  312.         tkWindow.drawable = NULL;
  313.     }
  314.     
  315.     if(!aglSetCurrentContext(tkWindow.context))
  316.     {
  317.         printf("TK error: Could not attach to context\n");
  318.         exit(1);
  319.     }
  320.  
  321.     return GL_TRUE;
  322. }
  323.  
  324. AGLPixelFormat tkGetPixelFmt(void)
  325. {
  326.     return tkWindow.fmt;
  327. }